package com.lian.anlimall.order.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import com.lian.anlimall.order.feign.MemberFeignService;
import com.lian.anlimall.order.vo.StatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lian.anlimall.order.entity.OrderEntity;
import com.lian.anlimall.order.service.OrderService;
import com.lian.common.utils.PageUtils;
import com.lian.common.utils.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 订单
 *
 * @author liananli
 * @email 377078237@qq.com
 * @date 2021-03-26 01:58:29
 */
@RestController
@RequestMapping("order/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private MemberFeignService memberFeignService;
    /**
     * 根据订单编号查询订单状态
     * @param orderSn
     * @return
     */
    @GetMapping(value = "/status/{orderSn}")
    public R getOrderStatus(@PathVariable("orderSn") String orderSn) {
        OrderEntity orderEntity = orderService.getOrderByOrderSn(orderSn);
        return R.ok().setData(orderEntity);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 分页查询当前登录用户的所有订单信息
     * @param params
     * @return
     */
    @PostMapping("/listWithItem")
    //@RequiresPermissions("order:order:list")
    public R listWithItem(@RequestBody Map<String, Object> params){
        PageUtils page = orderService.queryPageWithItem(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);
        return R.ok();
    }

    /**
     * 修改订单状态
     */
    @GetMapping("/updateOrderStatus")
    public void updateOrderStatus(@RequestParam("orderSn")String orderSn, HttpServletResponse response)throws IOException {
        orderService.updateOrderStatus1(orderSn);
//        return "redirect:http://member.anlimall.com/memberOrder.html";
         response.sendRedirect("http://member.anlimall.com/memberOrder.html");
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/fahuo")
    public R fahuo(@RequestBody Long id){

        orderService.fahuo(id);

        return R.ok();
    }

    @GetMapping(value = "/statistics")
    public R statistics(@RequestParam Map<String, Object> params){

        StatisticsVo statisticsVo = orderService.getstatistics(params);
        return R.ok().put("statisticsVo",statisticsVo);
    }
//    统计不同时间段的商品销量
    @GetMapping(value = "/statisticsbyTimeandSpu")
    public R statisticsbyTimeandSpu(@RequestParam Map<String, Object> params){

        StatisticsVo statisticsVo = orderService.getstatisticsbyTimeandSpu(params);
        return R.ok().put("statisticsVo",statisticsVo);
    }
}
